import sys
import tarfile
import time

import paramiko
'''remote_dir = "/usr/local/nginx/html/allure"
        username  = "root"
        password = "123456"
        hostname = "192.168.1.233"
'''

def  pack():
    tar =  tarfile.open("li.tar.gz",mode="w:gz")
    tar.add("report")


def   upload(filename):
    remote_dir = "/usr/local/nginx/html/allure"
    username  = "root"
    password = "123456"
    hostname = "192.168.1.233"
    client = paramiko.SSHClient()

    # 设置自动添加到系统的主机列表中
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    # 连接服务器
    client.connect(hostname=hostname,username=username,password=password,port=22)

    # 删除远程仓库的a.tar.gz报告文件
    # client.exec_command(f"rm -rf  {remote_dir}/a.tar.gz")
    # client.exec_command(f"rm -rf  {remote_dir}/*")
    # 上传文件包数据
    sftp = client.open_sftp()

    #  上传
    sftp.put("li.tar.gz",remote_dir+"/li.tar.gz")

    # 等待时间
    time.sleep(3)
    print("上传成功！！！！")
    # 解压
    client.exec_command(f"cd  {remote_dir}")
    time.sleep(1)
    client.exec_command(f"tar -xvf   {remote_dir}/li.tar.gz -C  {remote_dir}")
    time.sleep(2)

    sftp.close()
    client.close()





if __name__ == '__main__':
    pack()
    upload("li.tar.gz")






















